<?php
require 'session.php';

//db connect
require 'connect_pdo.php';
require 'classes.php';

$pdo = new pdo_query;

//login is required to access this page
include 'loginrequired.php';

//get required includes and declare class
require 'qrs_pdo.php';
include 'api.php';
include 'functions.php';

if (!empty($_POST['removeChar']))
{
	$removeId = $_POST['removeChar'];
	$remove = "DELETE FROM user_char where user_id = ? AND char_id = ?";
	$pdo->bound_query($remove,array($user_id,$char_id),'None');
	echo "Character removed from watch list- you can add them back in by searching for them again";
}

$pdo->bound_query($sqlSelectChars,array($user_id),'All');
$findCharResult=$pdo->rowcount;
$charRow = $pdo->result;

if ($findCharResult > 0)//found account characters
	{
	echo "<p>";
	echo "Account characters - Click the name for details<br/>";
	echo "Click the attribute/skill for a graph/recipe details</p>";
	echo "<table id='myTable' class='acclist tablesorter'><thead>";
	//print table headers
	if ($showPrimary == 0 && $showSecondary ==0)
		{echo "<tr class='highlight'><th>Remove</th><th>Name </th><th>Realm</th><th>Level</th><th>Race</th><th>Class</th><th>Faction</th><Th>Spec</Th><th>Last Login</th></tr></thead><tbody>";

		$numSkills = 0;//max skills to display

	}
	else if ($showPrimary == 0)
			{ echo "<tr class='highlight'><th>Remove</th><th>Name </th><th>Realm</th><th>Level</th><th>Race</th><th>Class</th><th>Faction</th><Th>Spec</Th><th>Skill 1</th><th>Skill 2</th><th>Skill 3</th><th>Skill 4</th><th>Last Login</th></tr></thead><tbody>";

			$numSkills = 4;//max skills to display
		}
	else if ($showSecondary == 0)
			{ echo "<tr class='highlight'><th>Remove</th><th>Name </th><th>Realm</th><th>Level</th><th>Race</th><th>Class</th><th>Faction</th><Th>Spec</Th><th>Profession 1</th><th>Profession 2</th><th>Last Login</th></tr></thead><tbody>";
			$numSkills = 2;//max skills to display
		}
	else
		{echo "<tr class='highlight'><th>Remove</th><th>Name </th><th>Realm</th><th>Level</th><th>Race</th><th>Class</th><th>Faction</th><Th>Spec</Th><th>Profession 1</th><th>Profession 2</th><th>Skill 1</th><th>Skill 2</th><th>Skill 3</th><th>Skill 4</th><th>Last Login</th></tr></thead><tbody>";}

	//for each character in the account
	foreach($charRow as $row)
	{

		//set the variables for basic character data
		$char=$row[0];
		$realm=$row[1];
		$race=$row[2];
		$class=$row[3];
		$faction=$row[4];
		$char_id=$row[5];

		//rebuild include variables for each character
		include 'accountvars.php';
		//get last history record for character
		$sql_s_acc_ch_hist = "SELECT level, last_login, guild, points, spec1,spec2,char_history_id FROM char_history ch
		WHERE char_id = ? ORDER BY last_login desc LIMIT 0,1";

		//set the variables for history data
		$pdo->bound_query($sql_s_acc_ch_hist,array($char_id),'All');
		$history = $pdo->result;

		$level=$history[0][0];
		$lastLogin=$history[0][1];
		$talent1=$history[0][4];
		$talent2=$history[0][5];
		$char_history_id = $history[0][6];

		//get last professions record for character
		if ($showPrimary == 0 && $showSecondary ==0)
			{$sqlSelectAccProfs = '';}

		else if ($showPrimary == 0)
				{$sqlSelectAccProfs = "SELECT pro_name, pro_val, pro_max FROM char_history_pro cp LEFT OUTER JOIN char_history ch ON cp.char_history_id = ch.char_history_id WHERE ch.char_id = ?
		AND ch.char_history_id = ?
		AND cp.pro_name NOT in ('Blacksmithing','Leatherworking','Alchemy','Tailoring','Engineering','Enchanting','Jewelcrafting','Inscription','Mining', 'Herbalism', 'Skinning')";}

		else if ($showSecondary==0)
				{$sqlSelectAccProfs = "SELECT pro_name, pro_val, pro_max FROM char_history_pro cp LEFT OUTER JOIN char_history ch ON
		cp.char_history_id = ch.char_history_id WHERE ch.char_id = ?
		AND ch.char_history_id = ?
		AND cp.pro_name in ('Blacksmithing','Leatherworking','Alchemy','Tailoring','Engineering','Enchanting','Jewelcrafting','Inscription','Mining', 'Herbalism', 'Skinning')";}

		else
			{$sqlSelectAccProfs = "SELECT pro_name, pro_val, pro_max FROM char_history_pro cp LEFT OUTER JOIN char_history ch ON cp.char_history_id = ch.char_history_id WHERE ch.char_id = ?
		AND ch.char_history_id = ?";

		}

		//create the table row for the character
		echo "<tr>";
		// show remove button
		echo "<td><form method='post'><input type='hidden' name='removeChar' value='$char_id'/><input type='submit' value='remove'/></form></td> ";
		//use the fakebutton form to link the character to a search of their information
		echo "<td>";
		echo "<a class='table' href='index.php?char=",$char,"&realm=",$realm,"'>",$char,"</a>";
		echo "</td>";
		echo "<td>",$realm,"</td>";
		echo "<td><a class='table' href='graph.php?char=$char&realm=$realm&type=level&ref=account'>",$level,"</a></td>";
		echo "<td>",$race,"</td><td>",$class,"</td><td>",$faction,"</td><td>",$talent1,"</td>";

		//display profession stuff
		if (!empty($sqlSelectAccProfs))
		{
			$pdo->bound_query($sqlSelectAccProfs,array($char_id,$char_history_id),'All');
			$proNum = $pdo->rowcount;
			$histRow = $pdo->result;
			
			foreach($histRow as $histPro)
			{
				//fill profession cells
				if ($histPro[0]<>'')//if a profession record exists
					{
					list ($expected, $colour, $cap) = profTarget($histPro[0],$level,$histPro[1]);//get variable values from      function
					$class = $colour;//set the element colour
					if ($cap > 0)
						{$title = 'Capped until level: ' . $cap;}//if capped, display the level
					else
						{$title = 'Expected level: ' . $expected;}//else display the expected level
					//if profession is in the professions array, show a link

					if (in_array($histPro[0],$profArray))
					{
						$link = "?char=$char&realm=$realm&prof=".$histPro[0];
						echo "<td title='$title' class='$class'>
					<a class='$class' href='professions.php$link'>",$histPro[0]," (", $histPro[1],"/",$histPro[2],")</a></td>";}
					else
						{echo "<td title='",$title,"' class='",$class,"'>",$histPro[0]," (", $histPro[1],"/",$histPro[2],")</td>";


					}
				}//end profession display
				else
				{
					echo "<td></td>";
				}
			}
		}
/*
		else
		{

		}
*/
		//check for missing skills/professions
		if ($proNum < $numSkills)
		{
			//write a blank cell for each missing profession
			$blankCell = $numSkills - $proNum;
			while ($blankCell > 0)
			{
				echo "<td></td>";
				$blankCell--;
			}
		}

		$warning = (date('U')-strtotime($lastLogin))/60/60/24;//days since last login
		//get warning days from preferences
		if ($warningPref > 0)
			{$warningBase = $warningPref;}
		else
			{$warningBase = 28;}//base value to warn at

		//build warning cell
		if ($warning>$warningBase)
			{$cellClass = 'warning';
			$cellTitle = 'This character has not logged in for over '. $warningBase .' days';
		}
		else
			{$cellClass= '';
			$cellTitle = '';
		}
		echo "<td class='$cellClass' title='$cellTitle'>" . $lastLogin . "</td>";//show last login date
		echo "</tr>";


	}//finished printing characters

	echo "</tbody><table>";

}
else//no characters in account
{
	echo "You have no characters attached to the account.";

}

include 'footer.html';

?>